package com.ujoku.controller;

import com.labillusion.core.platform.cdn.upyum.UpYun;
import com.labillusion.core.platform.web.rest.RESTController;
import com.ujoku.domain.TopBanner;
import com.ujoku.domain.TopBannerType;
import com.ujoku.service.CdnService;
import com.ujoku.service.TopBannerService;
import com.ujoku.view.domain.admin.TopBannerView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Greg.Chen on 14-11-12.
 */
@RestController
public class TopBannerController extends RESTController {

    @Autowired
    private CdnService cdnService;

    @Autowired
    private TopBannerService service;

    @RequestMapping(value = "/mobile/topBanner/list", method = RequestMethod.GET)
    @ResponseBody
    public TopBannerView list(){
        Map<String,Integer> query = new HashMap<>();
        query.put("type", TopBannerType.Mobile);

        List<TopBanner> topBannerList = service.selectList(query);
        TopBannerView topBannerView = getTopBannerView(topBannerList);

        return topBannerView;
    }

    @RequestMapping(value = "/website/topBanner/list", method = RequestMethod.GET)
    @ResponseBody
    public TopBannerView webBannerList(){

        Map<String,Integer> query = new HashMap<>();
        query.put("type",TopBannerType.WebSite);


        List<TopBanner> topBannerList = service.selectList(query);
        TopBannerView topBannerView = getTopBannerView(topBannerList);

        return topBannerView;
    }

    private TopBannerView getTopBannerView(List<TopBanner> topBannerList) {
        topBannerList.forEach(t ->{
            t.setTopBannerImage(cdnService.buildTopBannerImage(t.getId(),t.getUpdateTime().getTime()));
        });

        TopBannerView topBannerView = new TopBannerView();
        topBannerView.setList(topBannerList);
        return topBannerView;
    }
}
